package com.longdaji.decoration.ui.main.cart;

import android.content.Context;
import android.content.Intent;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.CheckBox;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.longdaji.decoration.R;
import com.longdaji.decoration.data.bean.CartBean;
import com.longdaji.decoration.ui.detail.GoodsDetailActivity;
import com.longdaji.decoration.utils.ImageUtil;
import com.longdaji.decoration.utils.StringUtil;
import com.longdaji.decoration.widget.MyElideTextView;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by longtao on 2018/1/31.
 */

public class CartAdapter extends RecyclerView.Adapter<CartAdapter.ViewHolder> {

    private final CartItemListener mListener;
    private Context mContext;
    private List<CartBean.Cart> mCartList = new ArrayList<>();
    private int selectSize = 0;

    public interface CartItemListener {

        void onCartItemClick(CartBean.Cart cartItem);

        void onCartCheckChange(int selectSize);

        void onChangeSkuNum(int position, long num);

    }

    public CartAdapter(Context context, List<CartBean.Cart> list, CartItemListener listener) {
        if (list != null) {
            this.mCartList = list;
        }
        this.mContext = context;
        this.mListener = listener;
    }

    public void setSelectAll() {
        for (CartBean.Cart cartItem : mCartList) {
            cartItem.setSelect(true);
        }
        selectSize = mCartList.size();
        notifyDataSetChanged();
    }

    public void setSelectAllCancel() {
        for (CartBean.Cart cartItem : mCartList) {
            cartItem.setSelect(false);
        }
        selectSize = 0;
        notifyDataSetChanged();
    }

    public int getDataSize() {
        return mCartList.size();
    }

    public List<String> getSelectDataId() {
        List<String> idList = new ArrayList<>();
        for (CartBean.Cart cartItem : mCartList) {
            if (cartItem.isSelect()) {
                idList.add(cartItem.getCartGoodsRecordId());
            }
        }
        return idList;
    }

    public List<CartBean.Cart> getSelectData() {
        List<CartBean.Cart> selectList = new ArrayList<>();
        for (CartBean.Cart cartItem : mCartList) {
            if (cartItem.isSelect()) {
                selectList.add(cartItem);
            }
        }
        return selectList;
    }

    public void setData(List<CartBean.Cart> list) {
        if (list != null) {
            this.mCartList = list;
        }
        notifyDataSetChanged();
    }

    public void removeData(List<CartBean.Cart> list) {
        if (list != null) {
            this.mCartList.remove(list);
        }
        notifyDataSetChanged();
    }

    public void setCartItemNum(int position, long num) {
        mCartList.get(position).setGoodsSkuNum(num);
        notifyDataSetChanged();
    }

    public CartBean.Cart getItemByPosition(int position) {
        return mCartList.get(position);
    }


    @Override
    public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_cart, parent, false);
        ViewHolder holder = new ViewHolder(view);
        return holder;
    }

    @Override
    public void onBindViewHolder(final ViewHolder holder, final int position) {
        final CartBean.Cart cartItem = mCartList.get(position);
       // ImageUtil.loadIntoUseFitWidth(mContext, cartItem.getGoodsSkuImg(), R.drawable.no_banner, holder.goodImg);
        ImageUtil.loadImage(mContext, cartItem.getGoodsSkuImg(), holder.goodImg);
        // GlideUtils.loadImage(mContext, cartItem.getGoodsSkuImg(), holder.goodImg, null, R.drawable.no_banner, R.drawable.no_banner);
        holder.goodsName.setText(cartItem.getGoodsName());
        holder.discountPrice.setText(StringUtil.formatNumber(cartItem.getGoodsSkuPrice()));
        if (cartItem.getGoodsSkuPrice() < cartItem.getPrice()) {
            holder.actualPrice.setText("¥" + StringUtil.formatNumber(cartItem.getPrice()));
        } else {
            holder.actualPrice.setVisibility(View.INVISIBLE);
        }
        holder.goodsNum.setText(cartItem.getGoodsSkuNum() + "");
        if (cartItem.isSelect()) {
            holder.select.setChecked(true);
        } else {
            holder.select.setChecked(false);
        }
        holder.select.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                boolean isSelect = cartItem.isSelect();
                cartItem.setSelect(!isSelect);
                notifyDataSetChanged();
                if (isSelect) {
                    selectSize--;
                } else {
                    selectSize++;
                }

                mListener.onCartCheckChange(selectSize);
            }
        });
        holder.minus.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if (cartItem.getGoodsSkuNum() > 1 && mListener != null) {
                    mListener.onChangeSkuNum(position, cartItem.getGoodsSkuNum() - 1);
                }
            }
        });

        holder.add.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if (mListener != null) {
                    mListener.onChangeSkuNum(position, cartItem.getGoodsSkuNum() + 1);
                }
            }
        });
        holder.cartItem.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent intent = GoodsDetailActivity.getStartIntent(mContext);
                intent.putExtra("goods_id", cartItem.getGoodsId());
                mContext.startActivity(intent);
            }
        });

    }

    @Override
    public int getItemCount() {
        return mCartList.size();
    }

    static class ViewHolder extends RecyclerView.ViewHolder {
        private MyElideTextView actualPrice;
        private TextView discountPrice;
        private TextView goodsName;
        private TextView goodsNum;
        private ImageView add;
        private ImageView minus;
        private ImageView goodImg;
        private LinearLayout cartItem;
        private CheckBox select;

        public ViewHolder(View itemView) {
            super(itemView);
            cartItem = itemView.findViewById(R.id.llyt_cart_item);
            goodsName = itemView.findViewById(R.id.tv_goods_name);
            discountPrice = itemView.findViewById(R.id.tv_discount_price);
            actualPrice = itemView.findViewById(R.id.tv_actual_price);
            minus = itemView.findViewById(R.id.iv_minus);
            goodsNum = itemView.findViewById(R.id.tv_num);
            add = itemView.findViewById(R.id.iv_add);
            select = itemView.findViewById(R.id.cb_select);
            goodImg = itemView.findViewById(R.id.iv_cart_good_img);
        }
    }

}
